package com.chaochis.wind.controller;

import com.chaochis.wind.base_config.page_config.NeedPageHelper;
import com.chaochis.wind.base_config.page_config.api.ApiResult;
import com.chaochis.wind.base_exceptions.serve_exception.DataHasDeleteException;
import com.chaochis.wind.base_exceptions.serve_exception.DataNotActiveException;
import com.chaochis.wind.base_exceptions.serve_exception.DataNotExistException;
import com.chaochis.wind.common_exceptions.not_exist_exception.AccountNotExistException;
import com.chaochis.wind.common_exceptions.not_exist_exception.UserNotExistException;
import com.chaochis.wind.common_exceptions.not_support_exception.LoginMethodNotSupportException;
import com.chaochis.wind.common_exceptions.other_login_exceptions.UserPasswordErrorException;
import com.chaochis.wind.params.wind_account_model.ReqAdminLoginAccount;
import com.chaochis.wind.params.wind_account_model.ReqSetBindUser;
import com.chaochis.wind.params.wind_account_model.ResponseAdminUser;
import com.chaochis.wind.service.WindAccountService;
import com.chaochis.wind.service.WindLoginService;
import com.chaochis.wind.service.WindUserService;
import com.chaochis.wind.vo.WindAccount;
import com.chaochis.wind.vo.WindUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/wind_account")
public class WindAccountController {

    @Autowired
    @Qualifier("windWebAdminLoginService")
    private WindLoginService windLoginService;

    @Autowired
    private WindAccountService windAccountService;

    @Autowired
    private WindUserService windUserService;


    @PostMapping("/login")
    public ApiResult<ResponseAdminUser> adminLogin(@RequestBody ReqAdminLoginAccount adminLoginAccount) throws DataNotExistException, DataHasDeleteException, DataNotActiveException, UserPasswordErrorException, LoginMethodNotSupportException {
        // 把请求参数转换成要查询的WindAccount对象
        System.out.println(adminLoginAccount);
        var loginAccount = new WindAccount();
        BeanUtils.copyProperties(adminLoginAccount, loginAccount);
        // 执行登录
        var token = this.windLoginService.login(loginAccount);
        var loginUser = this.windUserService.getTempUserInfoByToken(token);
        return ApiResult.success(new ResponseAdminUser(token, loginUser));
    }

    @GetMapping("/get_list")
    @NeedPageHelper
    public ApiResult<List<WindAccount>> getWindAccountList() {
        var windAccount = new WindAccount();
        var returnWindAccountList = this.windAccountService.getWindAccountList(windAccount);
        return ApiResult.success(returnWindAccountList);
    }

    @GetMapping("/get_bind_user")
    public ApiResult<WindUser> getBindWindUser(String accountId) {
        var windUser = this.windAccountService.getBoundWindUser(accountId);
        return ApiResult.success(windUser);
    }
    @PostMapping("/set_bind_user")
    public ApiResult<Boolean> setBindWindUser(ReqSetBindUser bindUser) throws AccountNotExistException, UserNotExistException {
        this.windAccountService.setBindUser(bindUser.getAccountId(), bindUser.getUserId());
        return ApiResult.success(true);
    }
    @PostMapping("/delete_account")
    public ApiResult<Boolean> deleteWindAccount(String accountId) throws AccountNotExistException {
        this.windAccountService.deleteWindAccount(accountId);
        return ApiResult.success(true);
    }
}